package com.dating.libs.web.services.datingservice;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.dating.activity.Dating;
import com.dating.pref.Constant;

public final class DatingService {
	public static final String _USER_ID = "USERID";
	public static final String _TOKEN = "AUTHENTICATETOKEN";
	public static final String _EMAIL = "EMAIL";
	public static final String _SESSIONID = "SESSIONID";
	public static final String _SECRET_KEY = "SECRET_KEY";

	public BasicInformation getUserInfo(int arg0) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getUserInfo");
		_client.addProperty("arg0", arg0 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _ret = (SoapObject) _envelope.bodyIn;
		SoapObject _ret1 = (SoapObject) _ret.getProperty("return");
		int _len = _ret1.getPropertyCount();
		BasicInformation _returned = new BasicInformation();
		for (int _i = 0; _i < _len; _i++) {
			_returned.setProperty(_i, _ret1.getProperty(_i));
		}
		return _returned;
	}

	public java.lang.String hello(java.lang.String name) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace, "hello");
		_client.addProperty("name", name);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return (java.lang.String) _envelope.getResponse();
	}

	public boolean uploadPhotos(java.lang.String arg0, java.lang.String arg1,
			java.lang.String arg2) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"uploadPhotos");
		_client.addProperty("arg0", arg0);
		_client.addProperty("arg1", arg1);
		_client.addProperty("arg2", arg2);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public ArrayList<String> getUserPhoto(int arg0, int arg1) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getUserPhoto");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		
		ArrayList<String> listImage=new ArrayList<String>();
		SoapObject _result=(SoapObject)_envelope.bodyIn;
		for(int _i=0;_i<_result.getPropertyCount();++_i){
			String _image=(_result.getProperty(_i)).toString();
			listImage.add(_image);
		}
		return listImage; // IMPORTANT!!!
	}

	public int getNumOfUserPhoto(int arg0, int arg1) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getNumOfUserPhoto");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return Integer.parseInt(_envelope.getResponse().toString());
	}

	public boolean editInformation(java.lang.String arg0, java.lang.String arg1)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"editInformation");
		_client.addProperty("arg0", arg0);
		_client.addProperty("arg1", arg1);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public ArrayList<UserProperties> getAllUserProperties(int arg0)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getAllUserProperties");
		_client.addProperty("arg0", arg0 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _result = (SoapObject) _envelope.bodyIn;
		int _size = _result.getPropertyCount();
		if (_size <= 0)
			return null;
		ArrayList<UserProperties> _array = new ArrayList<UserProperties>();
		Log.e("count soapobject", _size + "");
		for (int i = 0; i < _size; ++i) {
			UserProperties _t = new UserProperties();
			SoapObject _r = (SoapObject) _result.getProperty(i);
			String _name = _r.getPropertyAsString("name");
			String _value = _r.getPropertyAsString("value");
			if (Constant.ANY_TYPE.equals(_name)) {
				_name = "";
			}
			if (Constant.ANY_TYPE.endsWith(_value)) {
				_value = "";
			}
			_t.setName(_name);
			_t.setValue(_value);
			_array.add(_t);
		}
		return _array; // IMPORTANT!!!
	}

	public UserProperties getUserProperties(int arg0, java.lang.String arg1)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getUserProperties");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _ret = (SoapObject) _envelope.getResponse();
		int _len = _ret.getPropertyCount();
		UserProperties _returned = new UserProperties();
		for (int _i = 0; _i < _len; _i++) {
			_returned.setProperty(_i, _ret.getProperty(_i));
		}
		return _returned;
	}

	public boolean updateUserProperties(java.lang.String arg0,
			java.lang.String arg1) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"updateUserProperties");
		_client.addProperty("arg0", arg0);
		_client.addProperty("arg1", arg1);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean updateMyPosition(float arg0, float arg1) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"updateMyPosition");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public MatchingUser getBestMatching() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getBestMatching");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _ret = (SoapObject) _envelope.bodyIn;
		int _len = _ret.getPropertyCount();
		
		MatchingUser _returned=null;
		// num of usermatching
		for(int _j=0;_j<_len;++_j){
			SoapObject _tmp = (SoapObject) _ret.getProperty(_j);	
			int _count=_tmp.getPropertyCount();		// num of properties matching
			_returned = new MatchingUser();
			_returned.setProperty(0, _tmp.getProperty(0));
			_returned.setProperty(1, _tmp.getProperty(1));
			for (int _i = 2; _i < _count; _i++) {
				SoapObject _tmpPr=(SoapObject)_tmp.getProperty(_i);
				String name=_tmpPr.getPropertyAsString(0);
				String value=_tmpPr.getPropertyAsString(1);
				_returned.addMatchingProperties(name, value);
			}
		}
		
		return _returned;
	}

	public MatchingUser getNextMatching() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getNextMatching");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _ret = (SoapObject) _envelope.getResponse();
		int _len = _ret.getPropertyCount();
		MatchingUser _returned = new MatchingUser();
		for (int _i = 0; _i < _len; _i++) {
			_returned.setProperty(_i, _ret.getProperty(_i));
		}
		return _returned;
	}

	public ArrayList<AroundUser> getSinglesAroundMe(String lat, String lng, String distance,
			String offset, String lenght) throws Exception {
//		lat="21.004581";
//		lng="105.84582";
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getSinglesAroundMe");
		_client.addProperty("arg0", lat + "");
		_client.addProperty("arg1", lng + "");
		_client.addProperty("arg2", distance + "");
		_client.addProperty("arg3", offset + "");
		_client.addProperty("arg4", lenght + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject _result=(SoapObject)_envelope.bodyIn;
		
		ArrayList<AroundUser> _lstUser=new ArrayList<AroundUser>();
//		Log.e("num of around me", _result.getPropertyCount()+"");
		
		for(int _i=0;_i<_result.getPropertyCount();++_i){
			
			AroundUser _item=new AroundUser();
			int _userID=Integer.valueOf(((SoapObject)_result.getProperty(_i)).getProperty("userID").toString());
			String _distance=((SoapObject)_result.getProperty(_i)).getProperty("distance").toString();
			String _latitude=((SoapObject)_result.getProperty(_i)).getProperty("latitude").toString();
			String _longitude=((SoapObject)_result.getProperty(_i)).getProperty("longtitude").toString();
			Log.e("latitude", _latitude);
			Log.e("longitude", _longitude);
			
			_item.setUserID(_userID);
			_item.setDistance(Double.parseDouble(_distance));
			_item.setLatitude(_latitude);
			_item.setLongitude(_longitude);
			_lstUser.add(_item);
		}
		Log.e("num of around me", _lstUser.size()+"");
		return _lstUser; // IMPORTANT!!!
	}

	public UserInterest[] getUserInterest(int arg0, int arg1) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getUserInterest");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return null; // IMPORTANT!!!
	}

	public UserInterest[] getUserInterestYou(int arg0, int arg1)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getUserInterestYou");
		_client.addProperty("arg0", arg0 + "");
		_client.addProperty("arg1", arg1 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return null; // IMPORTANT!!!
	}

	public boolean interestHer(int arg0) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"interestHer");
		_client.addProperty("arg0", arg0 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean uploadPhotoViaLink(java.lang.String arg0) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"uploadPhotoViaLink");
		_client.addProperty("arg0", arg0);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public FbInterest[] getFBInterestByUser(int arg0) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"getFBInterestByUser");
		_client.addProperty("arg0", arg0 + "");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return null; // IMPORTANT!!!
	}

	public boolean userComeIn() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"userComeIn");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean userLogout() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"userLogout");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope);
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	/************************************ CONFIG ***********************************/
	private void addMapping(SoapSerializationEnvelope envelope) {
		envelope.addMapping(Configuration.wsNameSpace, "userInterest",
				new UserInterest().getClass()); // IMPORTANT!!!
		envelope.addMapping(Configuration.wsNameSpace, "properties",
				new Properties().getClass()); // IMPORTANT!!!
		envelope.addMapping(Configuration.wsNameSpace, "matchingUser",
				new MatchingUser().getClass()); // IMPORTANT!!!
		envelope.addMapping(Configuration.wsNameSpace, "aroundUser",
				new AroundUser().getClass()); // IMPORTANT!!!
		envelope.addMapping(Configuration.wsNameSpace, "basicInformation",
				new BasicInformation_bak().getClass()); // IMPORTANT!!!
	}

	private List<HeaderProperty> setHeader() {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty(_USER_ID, Dating.ME.UID));
		headerList.add(new HeaderProperty(_TOKEN, Dating.ME.AUTH));

		return headerList;
	}

	private List<HeaderProperty> setHeader(String userID, String token) {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty(_USER_ID, userID));
		headerList.add(new HeaderProperty(_TOKEN, token));

		return headerList;
	}

}
